package com.xyh.springboot.controller;

import com.xyh.springboot.exception.BizException;
import com.xyh.springboot.model.User;
import com.xyh.springboot.model.UserDetailPO;
import com.xyh.springboot.model.UserPo;
import com.xyh.springboot.service.IUserService;
import com.xyh.springboot.util.ResultCode;
import com.xyh.springboot.util.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author:kyle
 * @Date:2020/12/25 - 14:33
 * @Project:springboot
 * @Package:com.xyh.springboot.controller
 * @Version:1.0
 */
@RestController
@RequestMapping("/user1")
@Api(tags = "用户相关操作接口")//类描述

public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("/list")
    //SELECT user_pkid,username,password,nickname,reg_time,fk_detail_id,fk_dept_id,deleted
    //FROM t_user WHERE deleted=0//查找的是deleted=0未逻辑删除的user
    @ApiOperation("获得所有用户信息")//方法描述
    public Object getUsers(){

        //测试空指针异常
        String str = null;
        if(str.equals("aaa")){
            System.out.println("str = " + str);
        }
        
        List<User> users = userService.list();
        ResultVO success = ResultVO.success(users);
        return success;
    }

    @GetMapping("/get/{id}")
    //SELECT user_pkid,username,password,nickname,reg_time,fk_detail_id,fk_dept_id,deleted
    //FROM t_user WHERE user_pkid=? AND deleted=0
    @ApiOperation("通过主键id查询用户信息")
    @ApiImplicitParam(name = "id",value = "用户主键id",defaultValue = "1",required = true)
    public Object getUserById(@PathVariable("id") int id){
        //测试数据类型异常
        //int i = 5/0;

        if(id <= 0){

            throw new BizException(ResultCode.PARAM_IS_INVALID);
        }

        User user = userService.getById(id);
        return user;
    }

    @DeleteMapping("/delete/{id}")
    // 逻辑删除，其实时进行的update操作，
    // UPDATE t_user SET deleted=1 WHERE user_pkid=? AND deleted=0
    public Object deleteUserById(@PathVariable("id") Integer id){

        boolean b = userService.removeById(id);
        if(b){
            return ResultVO.success();
        }else{
            return ResultVO.fail(ResultCode.SERVER_INNER_ERROR);
        }
    }

    @PostMapping("/insert")
    public Object insertUser(User user){
//@RequestParam获取浏览器请求的参数
//        System.out.println("username = " + username);
//        System.out.println("password = " + password);
//        System.out.println("nickname = " + nickname);

//        User user = new User();
//        user.setUsername(username);
//        user.setPassword(password);
//        user.setNickname(nickname);
        user.setDeleted(0);
        userService.save(user);

        return user;
    }

    @PostMapping("/update")
    // UPDATE t_user SET username=?, nickname=?
    // WHERE user_pkid=? AND deleted=0
    public boolean updateUser(HttpServletRequest request){
        String sid = request.getParameter("id");
        Integer id = 0;
        if(id != null){
            id = Integer.parseInt(sid);
        }
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        System.out.println("id = " + id);
        System.out.println("username = " + username);
        System.out.println("nickname = " + nickname);
        User user = new User();
        user.setUserPkid(id);
        user.setUsername(username);
        user.setPassword(password);
        user.setNickname(nickname);
        boolean flag = userService.updateById(user);
        return flag;
    }

    //第一种处理异常方式，我们完全可以在出现异常的Controller中自定义注入
//    @ExceptionHandler(value = {java.lang.NullPointerException.class})
//    public ModelAndView handleNullPointerException(Exception e){
//        ModelAndView mv = new ModelAndView();
//        mv.setViewName("error");
//        mv.addObject("error",e);
//        return mv;
//    }
//
//    @ExceptionHandler(value = {java.lang.ArithmeticException.class})
//    public ModelAndView handleArithmeticException(Exception e){
//        ModelAndView mv = new ModelAndView();
//        mv.setViewName("error");
//        mv.addObject("error",e);
//        return mv;
//    }




    //刚开始入门测试
    @RequestMapping("/users")
    public ModelAndView getUsers(ModelAndView modelAndView){

        List<UserPo> users = new ArrayList<>();
        UserPo userPo1 = new UserPo(1,"熊大","1234");
        UserPo userPo2 = new UserPo(2,"熊二","4567");
        UserPo userPo3 = new UserPo(3,"熊三","9876");
        UserPo userPo4 = new UserPo(4,"熊四","4321");

        users.add(userPo1);
        users.add(userPo2);
        users.add(userPo3);
        users.add(userPo4);

        modelAndView.addObject("users",users);
        modelAndView.setViewName("users");
        return modelAndView;
    }

    @RequestMapping("/usersDetail")
    public String getUserDetail(ModelMap modelMap){
        List<UserPo> users = new ArrayList<>();
        UserDetailPO userDetailPO1 = new UserDetailPO(1,"成都",new Date());
        UserDetailPO userDetailPO2 = new UserDetailPO(2,"乐山",new Date());
        UserDetailPO userDetailPO3 = new UserDetailPO(3,"绵阳",new Date());
        UserDetailPO userDetailPO4 = new UserDetailPO(4,"广安",new Date());

        UserPo userPo1 = new UserPo(1,"熊大","1234",userDetailPO1);
        UserPo userPo2 = new UserPo(2,"熊二","4567",userDetailPO2);
        UserPo userPo3 = new UserPo(3,"熊三","9876",userDetailPO3);
        UserPo userPo4 = new UserPo(4,"熊四","4321",userDetailPO4);

        users.add(userPo1);
        users.add(userPo2);
        users.add(userPo3);
        users.add(userPo4);

        modelMap.addAttribute("usersDetail",users);
        return "usersDetail";//templates里面的静态文件，需要在controller里面跳转
    }
}
